package com.atgm.mall.gmmailmember.service.impl;

import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.MemberException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.R;
import com.atgm.mall.gmmailmember.dao.MemberThumbsGameCommentDao;
import com.atgm.mall.gmmailmember.entity.MemberThumbsGameCommentEntity;
import com.atgm.mall.gmmailmember.feign.ClubFeignService;
import com.atgm.mall.gmmailmember.service.MemberThumbsGameCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MemberThumbsGameCommentServiceImpl extends ServiceImpl<MemberThumbsGameCommentDao, MemberThumbsGameCommentEntity> implements MemberThumbsGameCommentService {

    @Autowired
    private ClubFeignService clubFeignService;

    @Override
    public Integer updateCommentThumbsInfo(Long commentId, Long memberId, Long gameId) {
        QueryWrapper<MemberThumbsGameCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        queryWrapper.eq("game_id", gameId);
        queryWrapper.eq("comment_id", commentId);
        queryWrapper.eq("member_id", memberId);
        MemberThumbsGameCommentEntity selectOne = baseMapper.selectOne(queryWrapper);
        int flag = 0;
        if (selectOne == null) {
            MemberThumbsGameCommentEntity memberThumbsGameCommentEntity = new MemberThumbsGameCommentEntity();
            memberThumbsGameCommentEntity.setGameId(gameId);
            memberThumbsGameCommentEntity.setCommentId(commentId);
            memberThumbsGameCommentEntity.setMemberId(memberId);
            memberThumbsGameCommentEntity.setDeleted(0);
            memberThumbsGameCommentEntity.setCreateTime(new Date());
            int count = baseMapper.insert(memberThumbsGameCommentEntity);
            if (count != 1) {
                throw new MemberException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
            }
            flag = 1;
        }else {
            int count = baseMapper.deleteById(selectOne.getId());
            if (count != 1) {
                throw new MemberException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
            }
        }

        //修改评论中的点赞数量
        R r = clubFeignService.updateGameUseFulCount(commentId, flag);
        if (0 != r.getCode()) {
            throw new MemberException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
        }
        return flag;
    }

    @Override
    public List<Long> queryIdsByMember(Long memberId, Long gameId) {
        QueryWrapper<MemberThumbsGameCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("game_id", gameId);
        queryWrapper.eq("member_id", memberId);
        List<MemberThumbsGameCommentEntity> list = baseMapper.selectList(queryWrapper);

        List<Long> outList = new ArrayList<>();
        if (GmMallUtils.isNotEmpty(list)) {
            outList = list.stream().map(MemberThumbsGameCommentEntity::getCommentId).collect(Collectors.toList());
        }
        return outList;
    }
}
